package com.gemme.gemme.wode;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.gemme.gemme.BaseActivity;
import com.gemme.gemme.R;
import com.gemme.gemme.login.GlideCircleTransform;
import com.gemme.gemme.manager.DataManager;
import com.gemme.gemme.shequ.ShequDetailActivity;
import com.gemme.gemme.util.StrUtil;
import com.gemme.gemme.view.RefreshView;
import com.gemme.gemme.view.Topview;
import com.jaeger.library.StatusBarUtil;

import java.util.HashMap;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

//侧滑删除地址
public class Zan2Activity extends BaseActivity{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zan);
        initTop();
        initView();
        getDataS();
    }


    RefreshView refreshView;
    private void initView() {
        refreshView = findViewById(R.id.refreshView);
        refreshView.setLayoutManager( new LinearLayoutManager(this));

    }


    private void initTop() {
//        StatusBarUtil.setColor(this,getResources().getColor(R.color.white_color) );
        StatusBarUtil.setTransparent(this);
//        StatusBarUtil.setTranslucent(this,250);
        Topview topview = findViewById(R.id.topview);
        topview.setContentText("赞");
    }


    public class ZanAdapter extends RecyclerView.Adapter<ZanAdapter.ViewHolder>{

        private List<ZanBean.DataBean.RecordsBean> records;

        public ZanAdapter(List<ZanBean.DataBean.RecordsBean> records) {

            this.records = records;
        }

        @NonNull
        @Override
        public ZanAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View inflate = LayoutInflater.from(parent.getContext()).
                    inflate(R.layout.zan_item, null);
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            inflate.setLayoutParams(layoutParams);
            return new ZanAdapter.ViewHolder(inflate);
        }

        @Override
        public void onBindViewHolder(@NonNull ZanAdapter.ViewHolder holder, int position) {
            try {
                ZanBean.DataBean.RecordsBean bean = records.get(position);
                String headImg = bean.getHeadImg();
                if(!TextUtils.isEmpty(headImg)){
                    Glide.with(mContext).load(headImg)
                            .transform(new GlideCircleTransform(mContext))
                            .into(holder.iv_head);
                }

                holder.tv_name.setText(StrUtil.noNUll(bean.getNickName()));
                holder.tv_desc.setText("评论了你的动态 " + StrUtil.getTime(bean.getCreateTime()));

                String coverPath = bean.getCoverPath();
                String videoPath = bean.getVideoPath();
                if(!TextUtils.isEmpty(coverPath)){
                    Glide.with(mContext).load(coverPath)
                            .into(holder.iv_pic);
                }
                if(!TextUtils.isEmpty(videoPath)){
                    Glide.with(mContext)
                            .setDefaultRequestOptions(
                                    new RequestOptions()
                                            .frame(1000000)
                                            .centerCrop()
                            )
                            .load(videoPath)
                            .into(holder.iv_pic);

                }
                holder.itemView.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        int id = bean.getId();
                        Intent intent = new Intent(mContext, ShequDetailActivity.class);
                        intent.putExtra("data",id);
                        startActivity(intent);
                    }
                });
            }catch (Exception e){

            }
        }

        @Override
        public int getItemCount() {
            return records.size();
        }



        public class ViewHolder extends RecyclerView.ViewHolder {
            ImageView iv_head;
            TextView tv_name;
            TextView tv_desc;
            ImageView iv_pic;
            public ViewHolder(@NonNull View itemView) {
                super(itemView);
                iv_head = itemView.findViewById(R.id.iv_head);
                tv_name = itemView.findViewById(R.id.tv_name);
                tv_desc = itemView.findViewById(R.id.tv_desc);
                iv_pic = itemView.findViewById(R.id.iv_pic);
            }
        }
    }


    private void getDataS() {
//        appgivelike/likedList
//        String url = DataManager.BaseUrl + ":9999/community/appcuserslike/likedList?current=1&size=1000";
        String url = DataManager.BaseUrl + "community/appgivelike/likedList?current=1&size=1000";

        HashMap<String, String> headMap = new HashMap<>();
        headMap.put(DataManager.AuthorizationKey,DataManager.getToken(mContext));
        DataManager.toServer2(mContext, url, DataManager.POST_TYPE, "", headMap, new DataManager.GeResponse() {
            @Override
            public void onResponse(String response) {
                try {
                    ZanBean zanBean = JSON.parseObject(response, ZanBean.class);
                    List<ZanBean.DataBean.RecordsBean> records = zanBean.getData().getRecords();
                    if(records == null)
                        return;
                    refreshView.setAdapter(new ZanAdapter(records));
                    System.out.println("");
                }catch (Exception e){

                }
                System.out.println("");
            }
        });
    }



}